package org.bite.lesson11;

/**
 * 一个线程做累加操作，另一个线程等待累加结束后打印结果
 */
public class Demo1100 {
    public static void main(String[] args) throws InterruptedException {
        Result result = new Result();
        Thread t = new Thread(){
            @Override
            public void run() {
                int sum = 0;
                for(int i = 1; i <= 1000; i++){
                    sum += i;
                }
                result.sum = sum;
                synchronized(result.locker){
                    result.locker.notify();
                }
            }
        };
        t.start();
        synchronized(result.locker){
            while(result.sum == 0){
                result.locker.wait();
            }
            System.out.println(result.sum);
        }
    }
}

/**
 * 中间变量，保存累加和
 */
class Result {
    //累加和
    public int sum;
    //锁对象
    public Object locker = new Object();
}
